package multi_thread.basic;

/**
 * Description:
 * yield() 是让当前线程从运行状态转变成就绪状态，不释放锁。只是cpu重新选择一次线程而已，有可能选到该线程。
 * yield 仅仅适合 debug，实际开发中不会使用到
 *
 * @author Cheng Jun
 * @version 1.0
 * @date 2019/10/30 11:49
 */
public class YieldExample {
    public static void main(String[] args) {
        MyThread myThread1 = new MyThread("线程1");
        MyThread myThread2 = new MyThread("线程2");
        myThread1.start();
        myThread2.start();
    }

    static class MyThread extends Thread {
        public MyThread(String name) {
            super(name);
        }

        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                System.out.println(i +" "+ this.getName());
                if (i % 3 == 0) {
                    Thread.yield();
                }
            }
        }
    }

}
